﻿using System;
using System.Windows.Forms;

namespace WS.FlowAuth.Lib.Test
{
  public partial class FrmAuth : Form
  {
    public FrmAuth(GetQrCodeArgs args)
    {
      _args = args;
      _args.host = "106.14.132.142:2009";
      _args.machineCode = "pppp";
      InitializeComponent();
    }

    private void btn_verify_Click(object sender, EventArgs e)
    {
      if (_code == null)
      {
        MessageBox.Show(@"请先生成二维码");
        return;
      }
      var auth_code = edt_auth_code.Text.Trim();
      if (string.IsNullOrWhiteSpace(auth_code))
      {
        MessageBox.Show(@"请输入授权码");
        return;
      }
      if (auth_code.Length != 8)
      {
        MessageBox.Show(@"授权码错误");
        return;
      }
      if (UserAuthCode.CheckAuthCode(auth_code, _args.optData, _code.RandomCode))
      {
        DialogResult = DialogResult.OK;
        Close();
        return;
      }
      MessageBox.Show(@"授权码错误");
    }

    private void btn_get_qrcode_Click(object sender, EventArgs e)
    {
      if (string.IsNullOrWhiteSpace(edt_usercode.Text))
      {
        MessageBox.Show(@"请输入用户代码");
        return;
      }
      _args.userCode = edt_usercode.Text;
      _code = UserAuthCode.GetQrCode(_args);
      pic_qrcode.Image = _code.QrCodeImage;
    }

    private readonly GetQrCodeArgs _args;
    private DTO.DTO_QrCode _code;
  }
}
